package com.javaclimb.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.javaclimb.music.domain.Singer;
import com.javaclimb.music.service.SingerService;
import com.javaclimb.music.vo.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

import static com.javaclimb.music.enums.ResponseEnum.*;

/**
 * @author 最酷的桃儿wjc
 * * @date 2022/5/12
 */
@RestController
@RequestMapping("/singer")
@Slf4j
public class SingerController {
    @Autowired
    private SingerService singerService;

    /**
     * 新增歌手
     * @param singer
     * @return
     */
    @PostMapping("/addsinger")
    public ResponseVo addSinger(Singer singer){
//        log.info("singer = {}",singer);
        return singerService.insertSinger(singer);
    }

    /**
     * 获取歌手列表
     * @return
     */
    @RequestMapping(value = "/singerlist",method = RequestMethod.GET)
    public Object getSingerList(){
        return singerService.getSingerList();
    }


    @RequestMapping(value = "/updateSingerPic",method = RequestMethod.POST)
    public ResponseVo updateSingerPic(@RequestParam("file")MultipartFile multipartFile,@RequestParam("id")int id){


        if (multipartFile.isEmpty()){
            return ResponseVo.error(ERROR_UPLOAD);
        }

        //获取上传文件的原文件名=当前时间到毫秒+原文件名
        String fileName = System.currentTimeMillis() + multipartFile.getOriginalFilename();
        //文件的路径
        String filePath = System.getProperty("user.dir")+System.getProperty("file.separator")+"img"+System.getProperty("file.separator")+"singerPic";
        //如果文件不存在，就要新增filePath
        File file1 = new File(filePath);
        if(!file1.exists()) {
            file1.mkdirs();
        }
        //实际的文件地址
        File dest = new File(filePath+System.getProperty("file.separator")+fileName);
        //存储到数据库里的相对文件地址
        String storeAvatorPath = "/img/singerPic/"+fileName;
        //文件上传
        try {
            multipartFile.transferTo(dest);
            Singer singer = new Singer();
            singer.setId(id);
            singer.setPic(storeAvatorPath);
            return singerService.updateSingerPic(singer,storeAvatorPath);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseVo.error(ERROR_UPLOAD);
        }
    }


    /**
     * 根据id查询歌手信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/getSingerById",method = RequestMethod.GET)
    public ResponseVo<Singer> getSingerById(Integer id){
        return singerService.getSingerById(id);
    }


    /**
     * 根据删除歌手信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteSingerById",method = RequestMethod.GET)
    public ResponseVo deleteSingerById(Integer id){
        return singerService.deleteSinger(id);
    }

    /**
     * 根据歌手名字模糊查询
     * @param name
     * @return
     */
    @GetMapping("/getSingerByName")
    public ResponseVo<List<Singer>> getSingerListByName(String name){
        return singerService.getSingersOfName(name);
    }

    /**
     * 更新歌手信息
     * @param singer
     * @return
     */
    @PostMapping("/updateSinger")
    public ResponseVo updateSinger(Singer singer){
        return singerService.updateSinger(singer);
    }

}
